package com.example.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.Result;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.query.TeacherQuery;
import com.example.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Anticipate
 * @since 2022-03-10
 */

@CrossOrigin // 解决跨域
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;


    @GetMapping("/findAll")
    public List<EduTeacher> findAllTeacher() {

/*
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            //e.printStackTrace();
            // 执行自定义异常
            throw new EduException(20001, "执行了自定义异常处理");
        }
*/

        List<EduTeacher> list = eduTeacherService.list(null);
        return list;
    }


    // 使用自定义的方法返回结果 链式编程
    @ApiOperation(value = "链式编程实现查找")
    @GetMapping("/selectAll")
    public Result selectAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("items", list);
    }


    // 逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public Result removeTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {

        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return Result.ok();
        } else
            return Result.error();
    }


    // 分页查询讲师的方法
    // current 当前页
    // limit 每页记录数
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result pageListTeacher(
            @PathVariable Long current,
            @PathVariable Long limit) {
        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        // 调用方法实现分页
        // 调用方法时，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal(); // 总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); // 数据List集合

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);

        return Result.ok().data(map);

        //return Result.ok().data("total", total).data("rows",records);

    }

    @ApiOperation(value = "条件查询带分页")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    /*
     * 使用@RequestBody(required = false)注解 提交方式必须使用post
     * */
    public Result pageTeacherCondition(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody(required = false) TeacherQuery teacherQuery) {

        // 1。创建对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

        // 2.构建条件对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        // 3.构建条件
        // 多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        // 判断条件是否为空， 如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            // 构建条件
            wrapper.like("name", name); // like 模糊查询
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);  // eq 等于
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin); // ge 大于等于
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end); // le 小于等于
        }

        wrapper.orderByDesc("gmt_create");
        // 4.调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher, wrapper);

        // 5.返回值
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return Result.ok().data("total", total).data("rows", records);
    }

    /*
     * 添加讲师的方法
     * */
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public Result addTeacher(
            @RequestBody() EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    /*
     * 根据讲师id查询
     * */
    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("/getTeacher/{id}")
    public Result getTeacherById(
            @PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return Result.ok().data("teacher", eduTeacher);
    }

    /*
     * 根据讲师id修改
     * 方法一：
     * */
    @ApiOperation(value = "根据讲师id修改1")
    @PutMapping("/updateTeacher/{id}")
    public Result updateTeacherById(

            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher) {

        eduTeacher.setId(id);
        eduTeacherService.updateById(eduTeacher);
        return Result.ok();
    }

    /*
     * 根据讲师id修改
     * 方法二：
     */
    @ApiOperation(value = "根据讲师id修改2")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(
            @RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

